import requests

from test.comm.log_tools import log
from test.comm.yaml_tools import YamlTools
from test.conf.env import URL



class BaseRequest:
    # 定义类变量
    sess = requests.session()
    # 1.定义发生请求的方法
    @classmethod
    def send_request(cls,**kwargs):
        try:
            rp = cls.sess.request(**kwargs)
        except Exception as e:
            log.error("接口请求失败")
        else:
            return rp

    @classmethod
    def execute_api_requests(cls,request,parametrize):
        method = request["method"]
        cookies = request["cookies"]
        headers = request["headers"]
        url = URL+request["url"]

        data = parametrize["data"]
        params = parametrize["params"]
        json = parametrize["json"]
        files = parametrize["files"]
        expect = parametrize["expect"]

        rp = cls.send_request(method = method,
                             cookies = cookies,
                             headers = headers,
                             url = url,
                             data = data,
                             params = params,
                             json = json,
                             files = files,
                             )
        return rp
if __name__ == '__main__':
    login_yaml_data = YamlTools.read_yaml_file('api_login_data.yaml')[0]
    rp = BaseRequest.execute_api_requests(login_yaml_data["request"],login_yaml_data["parametrize"][0])
    print(rp.text)
    print(login_yaml_data)


